package com.syqc.monitor.ui.list

import android.text.Html
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.utils.Language
import com.syqc.comlib.utils.loadStringDImgError
import com.syqc.custom.LeftRightTextView
import com.syqc.entity.GpsDetail
import com.syqc.login.LocationStatusModel
import com.syqc.monitor.R
import com.syqc.utils.ToolsUtil.stateColor

/** @Author Jasper @Time 2020/11/12 @Desc 列表监控适配器 */
class ListMonitorAdapter :
    BaseQuickAdapter<GpsDetail, BaseViewHolder>(R.layout.item_moitor_list) {
    private val isChinese = Language.isCurrentLanguage("zh_CN")
    var isPlate = true

    override fun convert(holder: BaseViewHolder, item: GpsDetail) {
        item.run {
            val popup = holder.getView<ImageView>(R.id.iv_list_popup)
            val carMap = holder.getView<ImageView>(R.id.iv_list_car_monitor)
            popup.setOnClickListener { mCallback?.callBack(item) }
            carMap.setOnClickListener { mCallback?.mapCallbacks(item) }
            carMap.setImageResource(getStateCar(state))

            holder.getView<ImageView>(R.id.iv_list_popup).setOnClickListener {
                mCallback?.callBack(item)
            }
            holder.getView<ImageView>(R.id.iv_list_logo_car)
                .loadStringDImgError(context, iconLink, R.mipmap.ic_car_location_no)
            val plate=holder.getView<TextView>(R.id.tv_list_name_plate)
            plate.text = if (isPlate) carPlate else carName
            plate.setBackgroundResource(getStatePlate(state))
            holder.setText(R.id.tv_address, addr ?: "")
            val lrTvTime = holder.getView<LeftRightTextView>(R.id.lr_tv_list_time)
            val lrTvState = holder.getView<LeftRightTextView>(R.id.lr_tv_list_state)
            val lrTvSpeed = holder.getView<LeftRightTextView>(R.id.lr_tv_list_speed)
            val lrTvDriver = holder.getView<LeftRightTextView>(R.id.lr_tv_list_driver)
            val lrTvMile = holder.getView<LeftRightTextView>(R.id.lr_tv_list_mile)
            val lrTvRunStop = holder.getView<LeftRightTextView>(R.id.lr_tv_list_run_time)
            val lrTvOffline = holder.getView<LeftRightTextView>(R.id.lr_tv_list_offline_time)
            val lrtvOil = holder.getView<LeftRightTextView>(R.id.lrtvOil)
            val lrtvWater = holder.getView<LeftRightTextView>(R.id.lrtvWater)
            val lrtvTemp = holder.getView<LeftRightTextView>(R.id.lrtvTemp)
            val lrtvDemp = holder.getView<LeftRightTextView>(R.id.lrtvDemp)
            val lrtvLoad = holder.getView<LeftRightTextView>(R.id.lrtvLoad)
            //离线时长
            if (offlineDuration.isNullOrEmpty()) {
                lrTvOffline.isVisible = false
            } else {
                lrTvOffline.isVisible = true
                lrTvOffline.setRightText(offlineDuration)
            }
            if (ListMonitorModel.instance.getTimeShow()) {
                lrTvTime.isVisible = true
                lrTvTime.setRightText(time)
            } else {
                lrTvTime.isVisible = false
            }
            if (!isChinese)
                lrTvState.getRightTv().textSize = 12f
            if (ListMonitorModel.instance.getStateShow()) {
                lrTvState.isVisible = true
                val string =
                    "<font color=${stateColor(state)} >$stateCn</font><font color='#666666'> | ACC$accTime</font>"
                lrTvState.setRightText(Html.fromHtml(string))
            } else {
                lrTvState.isVisible = false
            }
            if (ListMonitorModel.instance.getSpeedShow()) {
                lrTvSpeed.isVisible = true
                lrTvSpeed.setRightText("$speed${context.getString(R.string.unit_speed)}")
            } else {
                lrTvSpeed.isVisible = false
            }
            if (ListMonitorModel.instance.getDriverShow()) {
                lrTvDriver.isVisible = true
                lrTvDriver.setRightText(if (drvSign.isNullOrEmpty()) context.getString(R.string.desc_no_about_message) else drvSign)
            } else {
                lrTvDriver.isVisible = false
            }
            if (ListMonitorModel.instance.getMileShow()) {
                lrTvMile.isVisible = true
                lrTvMile.setRightText("$preMile ${context.getString(R.string.unit_mile)} / $mile ${context.getString(R.string.unit_mile)}")
            } else {
                lrTvMile.isVisible = false
            }
            if (ListMonitorModel.instance.getRunStopShow()) {
                lrTvRunStop.isVisible = true
                lrTvRunStop.setRightText(runStopTime)
            } else {
                lrTvRunStop.isVisible = false
            }
/////////////////////////////////////////////////////////////////////////////////////////////////////
            if (ListMonitorModel.instance.getOilShow()) {
                lrtvOil.isVisible = true
                lrtvOil.setRightText(LocationStatusModel.getDesc(st1))
            } else {
                lrtvOil.isVisible = false
            }
            if (ListMonitorModel.instance.getWaterShow()) {
                lrtvWater.isVisible = true
                lrtvWater.setRightText(LocationStatusModel.getDesc(st5))
            } else {
                lrtvWater.isVisible = false
            }
            if (ListMonitorModel.instance.getTempShow()) {
                lrtvTemp.isVisible = true
                lrtvTemp.setRightText(LocationStatusModel.getDesc(st3))
            } else {
                lrtvTemp.isVisible = false
            }
            if (ListMonitorModel.instance.getDempShow()) {
                lrtvDemp.isVisible = true
                lrtvDemp.setRightText(LocationStatusModel.getDesc(st4))
            } else {
                lrtvDemp.isVisible = false
            }
            if (ListMonitorModel.instance.getLoadShow()) {
                lrtvLoad.isVisible = true
                lrtvLoad.setRightText(LocationStatusModel.getDesc(st2))
            } else {
                lrtvLoad.isVisible = false
            }
        }
    }


    private fun getStateCar(state: String): Int {
        return when (state) {
            "2", "6" -> R.mipmap.ic_list_car_arreras
            "13" -> R.mipmap.ic_list_car_no
            "4", "8", "11", "12" -> R.mipmap.ic_list_car_alarm
            "1", "3" -> R.mipmap.ic_list_car_offline
            else -> R.mipmap.ic_list_car_monitor
        }
    }
    private fun getStatePlate(state: String): Int {
        return when (state) {
            "2", "6" -> R.drawable.bg_plate_arreras
            "13" -> R.drawable.bg_plate_no
            "4", "8", "11", "12" -> R.drawable.bg_plate_alarm
            "1", "3" -> R.drawable.bg_plate_offline
            else -> R.drawable.bg_plate_normal
        }
    }

    private var mCallback: ListMonitorCallback? = null
    fun setCallBack(mCallback: ListMonitorCallback) {
        this.mCallback = mCallback
    }

    interface ListMonitorCallback {
        fun callBack(item: GpsDetail)
        fun mapCallbacks(item: GpsDetail)
    }
}